Add optional module and filepath params to BaseChecker.add_message exclusive with node#10894
Add optional module and filepath params to BaseChecker.add_message exclusive with node#10894Pierre-Sassoulas wants to merge 3 commits intomainfrom
module and filepath params to BaseChecker.add_message exclusive with node#10894Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #10894 +/- ##
=======================================
Coverage 96.04% 96.04%
=======================================
Files 177 177
Lines 19627 19630 +3
=======================================
+ Hits 18850 18853 +3
Misses 777 777
🚀 New features to boost your workflow:
|
|
I'm wondering if we should raise if there's both a node and a filepath/module provided. As the result is going to be non sensical if we use the node lineno/end lineno on the other filepath. Maybe what we actually need is a list of "location descriptor" ?) |
DanielNoord
left a comment
There was a problem hiding this comment.
Makes sense to raise indeed!
Perhaps we can also help users with two overloads to distinguish between the two call patterns?
|
Brainstorming here, we don't want to break the add_message API it would be hell. So what about something like this: @dataclass
class MessageLocation:
module: str
filepath: str
line: int
col_offset: int = 0
end_lineno: int | None = None
end_col_offset: int | None = None
def add_message(
self,
msgid: str,
line: int | None = None,
node: NodeNG | None = None,
args: Any = None,
confidence: Confidence | None = None,
col_offset: int | None = None,
end_lineno: int | None = None,
end_col_offset: int | None = None,
additional_locations: list[NodeNG | MessageLocation] | None = None, # NEW
) -> None:
Then:
self.add_message(
"R0401",
line=5,
module="a_module",
filepath="/path/a.py",
args=("a -> b -> c -> a",),
additional_locations=[
import_node_in_b, # NodeNG
MessageLocation("c_module", "/path/c.py", line=3), # explicit
],
)
Duplicate code across 3 files:
self.add_message(
"R0801",
line=10,
module="module_a",
filepath="/path/a.py",
args=(3, duplicate_text),
additional_locations=[
MessageLocation("module_b", "/path/b.py", line=20, end_lineno=30),
MessageLocation("module_c", "/path/c.py", line=5, end_lineno=15),
],
) |
|
Do you think we really need the list? For me it would work if we just do We can enforce that with overloads to better document the API. |
Refs #10880 — allows checkers to override the reported module name and file path in message location, instead of relying on the current file context or node. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Covers the new `module` and `filepath` parameters that allow overriding the reported message location. Refs #10880 Co-Authored-By: Claude Opus 4.6 <[email protected]>
71ee66d to
aa03611
Compare
module and filepath params to BaseChecker.add_messagemodule and filepath params to BaseChecker.add_message exclusive with node
Add @overload signatures so type checkers enforce that `module` and `filepath` are only available when `node` is not provided. A runtime TypeError is raised if both are passed. Co-Authored-By: Claude Opus 4.6 <[email protected]>
|
If we raise message on multiple files we kinda need it. Right now we raise in one place with a bunch of text that talk about the other node/place. In text that the user has to read it make sense but in Json, sarif or for display in github interface a message for each location make a lot of sense.. i think we also need to have filepath/module for the existing node / the first location in any case as we'll never remove the node to replace by a list. (?) Now that I'm saying it making the node a 'Node | list[Node| message location]' would be incredible, except for the name that would'nt match. |
|
🤖 According to the primer, this change has no effect on the checked open source code. 🤖🎉 This comment was generated for commit 126df36 |
|
Having more messages depending on the output type doesn't sound like an API we should add. The current API makes sens to me but perhaps @jacobtylerwalls can weigh in? |
jacobtylerwalls
left a comment
There was a problem hiding this comment.
Thanks for the ping. I read the discussion on the other PR. I see why this could be helpful, but the "too-many-arguments" lints we're disabling here makes me wonder if we should slow down and factor this a little differently. We're trying to support a special case (reporting an error on some other location, in the similarity checker), so I would expect a dedicated method for that instead of overloading the existing add_message.
This feedback may not be welcome at this late point, so feel free to ignore.
|
I think Jacob is right. I explored if there were legitimate use case of giving a node then overriding the node's value. Seems like it's actually used in useless-else-on-loop (the else node doesn't exist in astroid see #10899), but most of the time it's an error that makes the info given worse at the cost of checking each time we add a message if something is overridden or not. Maybe we need to add two "optimized" function |
Type of Changes
Description
Required for a cleaner #10880. Allows checkers to override the reported module name and file path in message location, instead of relying on the current file context or node.